package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.PartBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin()
@RestController
@RequestMapping(value = "/auth")
public class PartController {
    @Autowired
    private PartBu partBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    /**
     * 查询部门表控制层
     */
    @GetMapping("/getAllPart")
    public SzResult getAllPart(@RequestParam(required = false) String partName,
                               @RequestParam(required = false) Integer page,
                               @RequestParam(required = false) Integer pageSize,
                               HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return partBu.getAllPart(page,pageSize,partName,token,plantId);
    }

    /**
     * 修改部门信息控制层
     */
    @PostMapping("/updatePart")
    @PermissionCheck("part_update")
    public SzResult updatePart(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        //        拿到token
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");
        String partName = jsonObject.getString("partName");
        String partRole = jsonObject.getString("partRole");
        String plantId = request.getHeader("Plant-Id");
        return partBu.updatePart(id, partName, partRole, token,plantId);
    }

    /**
     * 删除部门信息控制层
     */
    @PostMapping("/deletePart")
    @PermissionCheck("part_delete")
    public SzResult deletePart(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String id = jsonObject.getString("id");
        String plantId = request.getHeader("Plant-Id");
        return partBu.deletePart(id, token,plantId);
    }

    /**
     * 添加部门信息控制层
     */
    @PostMapping("/addPart")
    @PermissionCheck("part_add")
    public SzResult addPart(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String partName = jsonObject.getString("partName");
        String roleName = jsonObject.getString("roleName");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return partBu.addPart(partName, roleName, token,plantId);
    }

    /**
     * 获取DEFAULT_CODE_WEB值
     */
    @PostMapping("/getDefaultCodeWeb")
    @PermissionCheck("part_getDefaultCodeWeb")
    public SzResult getDefaultCodeWeb(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        //将请求体中的list集合拿出来
        List list = jsonObject.getJSONArray("list");
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return partBu.getDefaultCodeWeb(list, id, token,plantId);
    }
    @PostMapping("/updateDefaultCodeWeb")
    @PermissionCheck("part_updateDefaultCodeWeb")
    public SzResult updateDefaultCodeWeb(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        //将请求体中的list集合拿出来
        Integer number = jsonObject.getInteger("number");
        Integer ifOn = jsonObject.getInteger("ifOn");
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return partBu.updateDefaultCodeWeb(number, id,ifOn, token,plantId);
    }
   @PostMapping("/updateDefaultCodeApp")
   @PermissionCheck("part_updateDefaultCodeApp")
   public SzResult updateDefaultCodeApp(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
       //将请求体中的list集合拿出来
       Integer number = jsonObject.getInteger("number");
       Integer ifOn = jsonObject.getInteger("ifOn");
       String id = jsonObject.getString("id");
       String token = request.getHeader("Authorization");
       String plantId = request.getHeader("Plant-Id");
       return partBu.updateDefaultCodeApp(number, id, ifOn, token,plantId);
   }
    /**
     * 获取DEFAULT_CODE_APP值
     */
    @PostMapping("/getDefaultCodeApp")
    @PermissionCheck("part_getDefaultCodeApp")
    public SzResult getDefaultCodeApp(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        //将请求体中的list集合拿出来
        List list = jsonObject.getJSONArray("list");
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return partBu.getDefaultCodeApp(list, id, token,plantId);
    }
}
